package com.lanou.beautifulwardrobe.Brand.Adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.beautifulwardrobe.Application.BaseApplication;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.MyGridView;
import com.lanou.beautifulwardrobe.entify.EditRecommendBrand;
import com.lanou.beautifulwardrobe.entify.RiseData;

import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class BrandRiseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private int[] numTemp = {0, 2, 4};

    private List<RiseData> datas;
    private List<EditRecommendBrand> rankdatas;

    public void addData(List<RiseData> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }
    public void addRankData(List<EditRecommendBrand> rankdatas){
        this.rankdatas=rankdatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup view, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE0:
                View editView = LayoutInflater.from(view.getContext()).inflate(R.layout.brand_text_item, null);
                holder = new EditViewHolder(editView);
                break;
            case TYPE1:
                View RiseView = LayoutInflater.from(view.getContext()).inflate(R.layout.brand_risegridview_item, null);
                holder = new RiseViewHolder(RiseView);
                break;
            case TYPE2:
                View RankView = LayoutInflater.from(view.getContext()).inflate(R.layout.brand_rankgridview_item, null);
                holder = new RankViewHolder(RankView);
                break;
            case TYPE3:
                View RView = LayoutInflater.from(view.getContext()).inflate(R.layout.brand_rgridview_item, null);
                holder = new RViewHolder(RView);
                break;
            case TYPE4:
                View editRView = LayoutInflater.from(view.getContext()).inflate(R.layout.brand_recgridview_item, null);
                holder = new RecHolder(editRView);
                break;

        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE0:
                EditViewHolder editHolder = (EditViewHolder) holder;
                switch (position) {
                    case 0:
                        editHolder.textRec.setText("编辑推荐");
                        break;
                    case 2:
                        editHolder.textRec.setText("昨日上升最快品牌");
                        break;
                    case 4:
                        editHolder.textRec.setText("本周品牌排行");
                        break;
                    case 6:
                        editHolder.textRec.setText("品牌排行榜");
                        break;
                }

                break;
            case TYPE1:
                RiseViewHolder riseHolder = (RiseViewHolder) holder;
                if (datas != null && datas.size() > 0) {

                    riseHolder.riseAdapter.addData(datas.get(0).getItems());
                    riseHolder.riseGrid.setAdapter(riseHolder.riseAdapter);
                }
                break;
            case TYPE2:
                RankViewHolder rankHolder = (RankViewHolder) holder;
                if (datas != null && datas.size() > 0) {
                    rankHolder.rankAdapter.addData(datas.get(1).getItems());
                    rankHolder.rankGrid.setAdapter(rankHolder.rankAdapter);
                }
                break;
            case TYPE3:
                RViewHolder rHolder = (RViewHolder) holder;
                if (datas != null && datas.size() > 0) {
                    rHolder.rAdapter.addData(datas);
                    rHolder.rGrid.setAdapter(rHolder.rAdapter);
                }
                break;
            case TYPE4:
                RecHolder recHolder = (RecHolder) holder;
                if (datas != null && datas.size() > 0) {
                    recHolder.recAdapter.addData(rankdatas);
                    recHolder.recGrid.setAdapter(recHolder.recAdapter);
                }
                break;
        }

    }

    @Override
    public int getItemCount() {
        return 8;
    }

    public static final int TYPE0 = 0;
    public static final int TYPE1 = 1;
    public static final int TYPE2 = 2;
    public static final int TYPE3 = 3;
    public static final int TYPE4 = 4;

    @Override
    public int getItemViewType(int position) {

        if (position == 0 || position == 2 || position == 4 || position == 6) {
            return TYPE0;
        } else if (position == 1) {
            return TYPE4;
        } else if (position == 3) {
            return TYPE1;
        } else if (position == 5) {
            return TYPE2;
        } else
            return TYPE3;
    }

    public void onClick(View v) {
        Toast.makeText(BaseApplication.getContext(), "hahaha", Toast.LENGTH_SHORT).show();
    }

    class EditViewHolder extends RecyclerView.ViewHolder {
        private TextView textRec;

        public EditViewHolder(View view) {
            super(view);
            textRec = (TextView) view.findViewById(R.id.textRec);
        }
    }

    class RiseViewHolder extends RecyclerView.ViewHolder {
        private GridView riseGrid;
        private RiseGridViewAdapter riseAdapter;

        public RiseViewHolder(View view) {
            super(view);
            riseGrid = (GridView) view.findViewById(R.id.riseGrid);
            riseAdapter = new RiseGridViewAdapter();
        }
    }

    class RankViewHolder extends RecyclerView.ViewHolder {
        private GridView rankGrid;
        private RiseGridViewAdapter rankAdapter;

        public RankViewHolder(View view) {
            super(view);
            rankGrid = (GridView) view.findViewById(R.id.rankGrid);
            rankAdapter = new RiseGridViewAdapter();
        }
    }

    class RViewHolder extends RecyclerView.ViewHolder {
        private GridView rGrid;
        private RGridViewAdapter rAdapter;

        public RViewHolder(View view) {
            super(view);
            rGrid = (GridView) view.findViewById(R.id.rGrid);
            rAdapter = new RGridViewAdapter();
        }
    }

    class RecHolder extends RecyclerView.ViewHolder {
        private MyGridView recGrid;
        private EditGridViewAdapter recAdapter;

        public RecHolder(View view) {
            super(view);
            recGrid = (MyGridView) view.findViewById(R.id.recGrid);
            recAdapter = new EditGridViewAdapter();
        }

    }
}
